package com.ruoyi.workorder.service.impl;

import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.workorder.domain.WorkordersContactImage;
import com.ruoyi.workorder.mapper.WorkordersContactImageMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.workorder.mapper.WorkOrderMapper;
import com.ruoyi.workorder.domain.WorkOrder;
import com.ruoyi.workorder.service.IWorkOrderService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 工单Service业务层处理
 * 
 * @author zhaogang
 * @date 2024-07-23
 */
@Service
public class WorkOrderServiceImpl implements IWorkOrderService 
{
    @Autowired
    private WorkOrderMapper workOrderMapper;
    @Autowired
    private WorkordersContactImageMapper workordersContactImageMapper;
//    @Autowired
//    private MessageSendMapper messageSendMapper;
//    @Autowired
//    private MessageReceiveMapper messageReceiveMapper;

    /**
     * 查询工单
     * 
     * @param woId 工单主键
     * @return 工单
     */
    @Override
    public WorkOrder selectWorkOrderByWoId(Long woId)
    {
        return workOrderMapper.selectWorkOrderByWoId(woId);
    }

    /**
     * 查询工单列表
     * 
     * @param workOrder 工单
     * @return 工单
     */
    @Override
    public List<WorkOrder> selectWorkOrderList(WorkOrder workOrder)
    {
        return workOrderMapper.selectWorkOrderList(workOrder);
    }

    /**
     * 新增工单
     * 
     * @param workOrder 工单
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = {Exception.class, Error.class})
    public int insertWorkOrder(WorkOrder workOrder)
    {
        workOrder.setWoDate(new Date());
        String date = DateUtils.getDate();
        String str = workOrderMapper.selectWorkNumByDate(date);
        int number = 0;
        if (str != null) {
            // 使用substring和lastIndexOf方法获取最后的数字部分
            String numStr = str.substring(str.lastIndexOf("-") + 1);
            // 将提取的字符串数字转换为整型
            number = Integer.parseInt(numStr);
        }
        int j = number + 1;
        if (j < 10) {
            workOrder.setWoNumber("GD-" + date.replace("-", "") + "-000" + j);
        } else if (j < 100) {
            workOrder.setWoNumber("GD-" + date.replace("-", "") + "-00" + j);
        } else if (j < 1000) {
            workOrder.setWoNumber("GD-" + date.replace("-", "") + "-0" + j);
        } else {
            workOrder.setWoNumber("GD-" + date.replace("-", "") + "-" + j);
        }
        int i = workOrderMapper.insertWorkOrder(workOrder);
        if(i > 0){
            addWorkOrderImage(workOrder);
        }

        // TODO 新增工单
        return i;
    }

    /**
     * 修改工单
     * 
     * @param workOrder 工单
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = {Exception.class, Error.class})
    public int updateWorkOrder(WorkOrder workOrder)
    {
        int i = workOrderMapper.updateWorkOrder(workOrder);
        if(i > 0){
            // 工作联系单图片存储,删除图片
            String dbImages = workOrderMapper.selectWorkOrderByWoId(workOrder.getWoId()).getImages();
            // 获取图片差异
            String viewImages = workOrder.getImages();
            List<String> diffList = imageDiff(dbImages, viewImages);
            for (String picUrl : diffList) {
                String path = picUrl.substring(picUrl.indexOf("/", 1));
                File file = new File(RuoYiConfig.getProfile(), path);
                if (file.exists()) {
                    file.delete();
                }
            }
            // 删除数据库图片
            workordersContactImageMapper.deleteByWoId(workOrder.getWoId());
            // 添加图片
            addWorkOrderImage(workOrder);
        }
        return i;
    }

    /**
     *  比较两个字符串数组的差异
     * @param dbImages
     * @param viewImages
     * @return
     */
    public List<String> imageDiff(String dbImages, String viewImages) {
        List<String> result = new ArrayList<>();
        if (dbImages == null) {
            return result;
        }
        List<String> list1 = Arrays.asList(dbImages.split(","));
        if (viewImages == null) {
            return list1;
        }
        List<String> list2 = Arrays.asList(viewImages.split(","));
        // 求差集
        result = new ArrayList<>(list1);
        // 移除交集
        result.removeAll(list2);
        return result;
    }

    /**
     *  添加图片
     * @param workOrder
     */
    public void addWorkOrderImage (WorkOrder workOrder) {
        // 判断是否添加了图片
        if (workOrder.getImages() != null) {
            // 切分图片的路径
            String[] split = workOrder.getImages().split(",");
            for (String pic : split) {
                WorkordersContactImage workordersContactImage = new WorkordersContactImage();
                workordersContactImage.setWoId(workOrder.getWoId());
                workordersContactImage.setWoImageSrc(pic);
                workordersContactImage.setWoImageDate(new Date());
                workordersContactImageMapper.insertWorkordersContactImage(workordersContactImage);
            }
        }
    }

    /**
     * 批量删除工单
     *
     * @param woIds 需要删除的工单主键
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = {Exception.class, Error.class})
    public int deleteWorkOrderByWoIds(Long[] woIds)
    {
        System.out.println("woIds ******************** " +  woIds.length);
        // 删除数据库图片
        workordersContactImageMapper.deleteByWoIds(woIds);
        // 删除工单
        return workOrderMapper.deleteWorkOrderByWoIds(woIds);
    }


    /**
     * 删除工单信息
     * 
     * @param woId 工单主键
     * @return 结果
     */
    @Override
    public int deleteWorkOrderByWoId(Long woId)
    {
        return workOrderMapper.deleteWorkOrderByWoId(woId);
    }

}
